//
//  MineFavorTableViewController.m
//  project_delicious
//
//  Created by dlios on 15-3-31.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "MineFavorTableViewController.h"
#import "DataBaseHandler.h"
#import "Food.h"
#import "TheFoodDetailsViewController.h"
#import "FavorCell.h"
#import "FoodDetailsViewController.h"
#import "LatDetailsViewController.h"
#import "UIColor+AddColor.h"

@interface MineFavorTableViewController ()
@property (nonatomic, retain) NSMutableArray *favorArr;
@end

@implementation MineFavorTableViewController
- (void)dealloc
{
    [_favorArr release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    
    
    
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.translucent = NO;
    self.tabBarController.tabBar.hidden = YES;
    
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"backToRecomm.png"] style:UIBarButtonItemStylePlain target:self action:@selector(back)] autorelease];
    
    [self.tableView registerClass:[FavorCell class] forCellReuseIdentifier:@"yu"];
    NSLog(@"%@", [[DataBaseHandler shareInstance] selectAll]);
    self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"IMG_1345.JPG"]];
    self.tableView.separatorColor = [UIColor clearColor];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [self.navigationController popViewControllerAnimated:YES];
}
//back
- (void)back
{
    [self.navigationController popViewControllerAnimated:NO];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return self.favorArr.count;
}

//cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 66;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FavorCell *cell = [tableView dequeueReusableCellWithIdentifier:@"yu" forIndexPath:indexPath];
    Food *food = [self.favorArr objectAtIndex:indexPath.row];
    
    cell.favLabel.text = food.nameLabel;
    cell.backgroundColor = [UIColor clearColor];
    
    // Configure the cell...
    
    return cell;
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.favorArr = [[DataBaseHandler shareInstance] selectAll];
    [self.tableView reloadData];
    self.navigationController.navigationBar.hidden = NO;
    self.tabBarController.tabBar.hidden = YES;
    if (self.favorArr.count == 0) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"快去看看" message:@"亲, 您还没有收藏哦" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alertView show];
        [self.view addSubview:alertView];
        [alertView release];
    }
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    Food *food = [self.favorArr objectAtIndex:indexPath.row];
    
   
    if (food.foodStr.length < 20 ){
        LatDetailsViewController *latDe = [[LatDetailsViewController alloc] init];
        latDe.num = food.foodStr;
        //
        self.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:latDe animated:YES];
        [latDe release];
        
   } else {
       NSString *str = [food.foodStr substringWithRange:NSMakeRange(44, 4)];
       if ([str isEqualToString:@"more"]) {
           FoodDetailsViewController *foodDe = [[FoodDetailsViewController alloc] init];
           foodDe.nameTitle = food.nameLabel;
           foodDe.str = food.foodStr;
           //
           self.hidesBottomBarWhenPushed = YES;
           [self.navigationController pushViewController:foodDe animated:YES];
           [foodDe release];
           
       } else {
        TheFoodDetailsViewController *foodDe = [[TheFoodDetailsViewController alloc] init];
        foodDe.nameTitle = food.nameLabel;
        foodDe.str = food.foodStr;
        foodDe.str1 = food.foodStr1;
        foodDe.fid = food.cookStr;
           //
           self.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:foodDe animated:NO];
        [foodDe release];
        
       }
    
   }
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
